$(function(){
    if (GetLS("user") == null && window.location.href.indexOf("signup.html") == -1){
        if (window.location.href.indexOf("findpassword.html") == -1){
            window.location.href = "/signup.html";
        }
    }    
    var img = Id2url(CurrentUser);
    var name = CurrentUser.name?CurrentUser.name:"";
    //头部区域
    var header ="<div class='navbar'>" + 
                "    <div class='navbar-inner'>" + 
                "        <div class='container-fluid'>" + 
                "            <a class='brand' href='index.html'>" + 
                "                <i class='icon-heart-empty'></i>" + 
                "                <span class='hidden-phone'>IT工程师后台管理系统</span>" + 
                "            </a>" + 
                "            <a class='toggle-nav btn pull-left'>" + 
                "                <i class='icon-reorder'></i>" + 
                "            </a>" + 
                "            <ul class='nav pull-right'>" + 
                "                <li id='colorsel' class='dropdown light only-icon'>" + 
                "                    <a class='dropdown-toggle' data-toggle='dropdown'>" + 
                "                        <i class='icon-adjust'></i>" + 
                "                    </a>" + 
                "                    <ul class='dropdown-menu color-settings'>" + 
                "                        <li class='color-settings-contrast-color'>" + 
                "                            <a data-change-to='contrast-red'><i class='icon-adjust text-red'></i> 红</a>" + 
                "                            <a data-change-to='contrast-blue'><i class='icon-adjust text-blue'></i> 蓝</a>" + 
                "                            <a data-change-to='contrast-orange'><i class='icon-adjust text-orange'></i> 橙</a>" + 
                "                            <a data-change-to='contrast-purple'><i class='icon-adjust text-purple'></i> 紫</a>" + 
                "                            <a data-change-to='contrast-green'><i class='icon-adjust text-green'></i> 绿</a>" + 
                "                            <a data-change-to='contrast-muted'><i class='icon-adjust text-muted'></i> 灰</a>" + 
                "                            <a data-change-to='contrast-fb'><i class='icon-adjust text-fb'></i> 深蓝</a>" + 
                "                            <a data-change-to='contrast-dark'><i class='icon-adjust text-dark'></i> 黑</a>" + 
                "                            <a data-change-to='contrast-pink'><i class='icon-adjust text-pink'></i> 粉</a>" + 
                "                            <a data-change-to='contrast-grass-green'><i class='icon-adjust text-grass-green'></i> 草绿</a>" + 
                "                            <a data-change-to='contrast-sea-blue'><i class='icon-adjust text-sea-blue'></i> 海蓝</a>" + 
                "                            <a data-change-to='contrast-banana'><i class='icon-adjust text-banana'></i> 香蕉黄</a>" + 
                "                            <a data-change-to='contrast-dark-orange'><i class='icon-adjust text-dark-orange'></i> 深橙</a>" + 
                "                            <a data-change-to='contrast-brown'><i class='icon-adjust text-brown'></i> 棕</a>" + 
                "                        </li>" + 
                "                    </ul>" + 
                "                </li>" + 
                "                <li id='personel' class='dropdown dark user-menu'>" + 
                "                    <a class='dropdown-toggle' data-toggle='dropdown'>" + 
                "                        <img height='23' src='" + img + "' width='23' />" + 
                "                        <span class='user-name hidden-phone'>" + name + "</span>" + 
                "                        <b class='caret'></b>" + 
                "                    </a>" + 
                "                    <ul class='dropdown-menu'>" + 
                "                        <li>" + 
                "                            <a href='user_Profile.html'><i class='icon-user'></i>个人中心</a>" + 
                "                        </li>" + 
                "                        <li class='divider'></li>" + 
                "                        <li>" + 
                "                            <a id='exit'><i class='icon-signout'></i>退出登录</a>" + 
                "                        </li>" + 
                "                    </ul>" + 
                "                </li>" + 
                "            </ul>" + 
                "        </div>" + 
                "    </div>" + 
                "</div>";
    $("#header").html(header);            
    //左侧导航菜单
	var html = '<ul class="nav nav-stacked">';
	var menu = Menu[CurrentUser.category];
    var length = menu && menu.length || 0;
	for (var i = 0; i < length; i++){
		if (menu[i].url){
			html += '<li class="active">' + 
	                '    <a href="' + menu[i].url + '">' + 
	                '        <i class="icon-' + menu[i].icon + '"></i>' + 
	                '        <span>' + menu[i].title + '</span>' + 
	                '    </a>' + 
	                '</li>';
		} else {
			html += '<li class="">' + 
	                '    <a class="dropdown-collapse" href="#">' + 
	                '        <i class="icon-' + menu[i].icon + '"></i>' + 
	                '        <span>' + menu[i].title + '</span>' + 
	                '        <i class="icon-angle-down angle-down"></i>' + 
	                '    </a>' + 
	                '    <ul id="' + menu[i].icon + '" class="nav nav-stacked">';
	        for (var j = 0; j < menu[i].child.length; j ++){
	        	html += '<li id="' + menu[i].child[j].icon + '" class="">' + 
                        '    <a href="' + menu[i].child[j].url + '?ptab=' + menu[i].icon + '&ctab=' + menu[i].child[j].icon + '">' + 
                        '        <i class="icon-' + menu[i].child[j].icon + '"></i>' + 
                        '        <span>' + menu[i].child[j].title + '</span>' + 
                        '    </a>' + 
                        '</li>';
	        }      
	        html += '</ul>' + 
                '</li>';  
		}
	}
	html += '</ul>';
	$("#guide").html(html);
    $("#" + getValue("ptab")).css("display","block");
    $("#" + getValue("ctab")).addClass("active");
	//切换主题
	var themeclass = GetLS("theme")?GetLS("theme"):"contrast-green";
	$('body').addClass(themeclass);

	$("#colorsel").click(function(){ 
    	$(this).toggleClass("open");
    });
    $("#personel").click(function(){ 
    	$(this).toggleClass("open");
    });
    $(".color-settings-contrast-color>a").hover(function(){
    	$('body')[0].className = $('body')[0].className.replace(/(^|\s)contrast.*?(\s|$)/g, ' ').replace(/\s\s+/g, ' ').replace(/(^\s|\s$)/g, '');
        $('body').addClass($(this).data("change-to"));
        SetLS("theme", $(this).data("change-to"));
    });
    $("#exit").click(function(){
        $ajax({
            url:"/user/out",
            success:function(res){
                SetLS("user",null);
                window.location.href = "signup.html";
            }
        });
    });
	init();
});
function init(){ 
	var body, content, nav, nav_closed_width, nav_open, nav_toggler;

    nav_toggler = $("header .toggle-nav");
    nav = $("#main-nav");
    content = $("#content");
    body = $("body");
    nav_closed_width = 50;
    nav_open = body.hasClass("main-nav-opened") || nav.width() > nav_closed_width;
    $("#main-nav .dropdown-collapse").on("click", function(e) {
        var link, list;

        e.preventDefault();
        link = $(this);
        list = link.parent().find("> ul");

        if (list.is(":visible")) {
            if (body.hasClass("main-nav-closed") && link.parents("li").length === 1) {
                false;
            } else {
                link.removeClass("in");
                list.slideUp(300, function() {
                    return $(this).removeClass("in");
                });
            }
        } else {
            link.addClass("in");
            list.slideDown(300, function() {
                return $(this).addClass("in");
            });
        }
        return false;
    });

    nav_toggler.on("click", function() {
        if (nav_open) {
            $(document).trigger("nav-close");
        } else {
            $(document).trigger("nav-open");
        }
        return false;
    });
    $(document).bind("nav-close", function(event, params) {
        body.removeClass("main-nav-opened").addClass("main-nav-closed");
        return nav_open = false;
    });
    return $(document).bind("nav-open", function(event, params) {
        body.addClass("main-nav-opened").removeClass("main-nav-closed");
        return nav_open = true;
    });
}